/*==============================================================================
案例3：零售销售额预测（Lasso回归）
文件: case03_sales_forecasting.do
==============================================================================*/

clear all
set more off
capture log close

* 设置工作目录
cd "/Users/mac/git/stata"

* 创建输出目录
capture mkdir output
capture mkdir output/figures

* 开始日志
log using "output/case03_sales_forecasting.log", replace text

display "=========================================="
display "案例3：零售销售额预测"
display "=========================================="

/*------------------------------------------------------------------------------
第一步：数据准备
------------------------------------------------------------------------------*/

* 设置随机种子
set seed 12345

* 创建模拟数据
set obs 500

* 门店特征
gen store_size = rnormal(1000, 300)  // 门店面积
gen foot_traffic = rpoisson(500)     // 日客流量
gen competitors = int(runiform() * 5) + 1  // 竞争对手数量
gen parking_spaces = int(rnormal(50, 15))  // 停车位数量
gen distance_metro = runiform() * 5  // 距地铁距离（公里）

* 营销变量
gen ad_spend = rnormal(10000, 3000)  // 广告支出
gen promotion_days = int(runiform() * 30)  // 促销天数
gen loyalty_members = int(rnormal(500, 150))  // 会员数量

* 地区变量
gen region = int(runiform() * 4) + 1  // 地区（1-4）
tabulate region, generate(region_)

* 创建一些噪声变量（不相关）
forvalues i = 1/10 {
    gen noise_`i' = rnormal(0, 1)
}

* 生成销售额（真实关系）
gen sales = 50000 + ///
    30 * store_size + ///
    80 * foot_traffic - ///
    5000 * competitors + ///
    100 * parking_spaces - ///
    2000 * distance_metro + ///
    2 * ad_spend + ///
    500 * promotion_days + ///
    50 * loyalty_members + ///
    rnormal(0, 10000)

replace sales = max(0, sales)

* 数据摘要
summarize sales store_size foot_traffic competitors ad_spend

/*------------------------------------------------------------------------------
第二步：OLS回归（基准模型）
------------------------------------------------------------------------------*/

display ""
display "第二步：OLS回归（包含所有变量）"
display "------------------------------"

regress sales store_size foot_traffic competitors parking_spaces ///
    distance_metro ad_spend promotion_days loyalty_members ///
    region_* noise_*

display ""
display "OLS R² = " %5.4f e(r2)
display "包含变量数: " e(df_m)

/*------------------------------------------------------------------------------
第三步：Lasso回归（特征选择）
------------------------------------------------------------------------------*/

display ""
display "第三步：Lasso回归（自动特征选择）"
display "--------------------------------"

lasso linear sales store_size foot_traffic competitors parking_spaces ///
    distance_metro ad_spend promotion_days loyalty_members ///
    region_* noise_*, ///
    selection(cv, folds(5)) rseed(123)

display ""
display "Lasso选择的变量："
lassocoef, display(coef, standardized)

* 使用选中的变量重新估计
lasso linear sales store_size foot_traffic competitors parking_spaces ///
    distance_metro ad_spend promotion_days loyalty_members ///
    region_* noise_*, ///
    selection(cv) rseed(123)

predict sales_lasso, xb

* 计算性能
gen error_lasso = sales - sales_lasso
gen error_sq_lasso = error_lasso^2
quietly summarize error_sq_lasso
scalar rmse_lasso = sqrt(r(mean))

quietly correlate sales sales_lasso
scalar r2_lasso = r(rho)^2

display ""
display "Lasso性能："
display "  R² = " %5.4f r2_lasso
display "  RMSE = " %10.2f rmse_lasso

/*------------------------------------------------------------------------------
第四步：管理洞察
------------------------------------------------------------------------------*/

display ""
display "=========================================="
display "管理洞察和建议"
display "=========================================="
display ""

display "1. 关键销售驱动因素（Lasso识别）"
display "   ① 客流量 - 最重要"
display "   ② 门店面积 - 显著正向"
display "   ③ 竞争对手数量 - 负向影响"
display "   ④ 广告支出 - 正向影响"
display ""

display "2. 门店选址建议"
display "   - 优先选择高客流量地段"
display "   - 避免竞争过度激烈区域"
display "   - 确保足够停车位"
display "   - 靠近地铁站"
display ""

display "3. 营销策略"
display "   - 增加广告投入有正向回报"
display "   - 促销活动提升销售"
display "   - 发展会员计划"
display ""

log close
